package com.yoomiito.app.ui.my.remain.withdraw

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter.OnItemChildClickListener
import com.yoomiito.app.R
import com.yoomiito.app.adapter.withdraw.WithdrawListAdapter
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.my.ApplyInfo
import com.yoomiito.app.utils.AllCode
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.EventMessage
import com.yoomiito.app.widget.SpacingDecoration
import kotlinx.android.synthetic.main.act_deduct_money.rcy
import kotlinx.android.synthetic.main.act_deduct_money.smartRefreshLayout
import kotlinx.android.synthetic.main.activity_my_remain.btn_submit
import kotlinx.android.synthetic.main.refresh_rcy_layout.refreshLayout
import kotlinx.android.synthetic.main.title_bar.iv_back_left
import kotlinx.android.synthetic.main.title_bar.tv_center

/**
 * 可抵扣余额
 */
class WithdrawListActivity : BaseActivity<WithdrawListPresenter>() {

    private var mAdapter: WithdrawListAdapter? = null
    private var mCurrentPage = 1
    var empty: View? = null
    var headerView: View? = null
    var type : Int = 1

    companion object {
        fun start(context: Context,type:Int) {
            val intent = Intent(context, WithdrawListActivity::class.java)
            intent.putExtra("types",type)
            context.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int = R.layout.act_withdraw_list

    override fun newP(): WithdrawListPresenter = WithdrawListPresenter(App.mAppConfig)

    override fun initData(savedInstanceState: Bundle?) {
        initView()
    }

    override fun onResume() {
        super.onResume()
        requestData()
    }

    private fun initView() {
        iv_back_left.setOnClickListener { finish() }
        tv_center.text = "提现"
        smartRefreshLayout.setEnableRefresh(false)
        smartRefreshLayout.setOnLoadMoreListener {
            if (!mAdapter?.isLoading!!) {
                mCurrentPage++
                requestData()
            }
        }
        rcy.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
        rcy.addItemDecoration(SpacingDecoration(0, DensityUtil.dp2px(1f), true))
        mAdapter = WithdrawListAdapter(this)
        rcy.adapter = mAdapter
        empty = LayoutInflater.from(context).inflate(R.layout.item_live_empty, null, false)
        val text = empty?.findViewById<TextView>(R.id.item_empty_tv)
        text?.text = "暂无提现方式"

        mAdapter?.onItemChildClickListener = object : OnItemChildClickListener {
            override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {


            }

        }
        mAdapter?.setOnItemClickListener { adapter, view, position ->
            mAdapter?.data?.get(position)?.let { WithdrawActivity.start(this@WithdrawListActivity, it,type) }
        }
        headerView = LayoutInflater.from(this).inflate(R.layout.view_withdraw_head, null, false)


        btn_submit.setOnClickListener {
            AddWithdrawActivity.start(this,type)
        }
    }

    override fun eventMessage(eventMessage: EventMessage?) {
        super.eventMessage(eventMessage)
        if (eventMessage?.eventType.equals("withdraw")){
            finish()
        }
    }

    private fun requestData() {
        type = intent.getIntExtra("types",1)
        p.accountList(type)
    }

    fun setData(t: List<ApplyInfo>?) {
        if (t.isNullOrEmpty()) {
            mAdapter?.emptyView = empty
        } else {
            mAdapter?.removeAllFooterView()
            mAdapter?.addFooterView(headerView)
            mAdapter?.setNewData(t)
        }
        smartRefreshLayout.finishLoadMore()
    }
}